package vv.lines.core.context;

import vv.lines.store.IChampionsStore;
import vv.lines.store.jaxb.JaxbChampionsStore;
import vv.lines.view.ViewFactory;
import vv.lines.view.swing.SwingViewFactory;

import java.io.File;

import static vv.lines.utils.LinesConstants.*;

/**
 * @author vpotapenko
 */
public class ContextBuilder {

    private static final String CHAMPIONS_FILENAME = "champions.xml";

    private String linesHome;
    private int maxValue;
    private int rowCount;
    private int columnCount;
    private int clearableSize;
    private int maxChampion;
    private int generateCount;

    private ViewFactory viewFactory;
    private IChampionsStore championStore;

    public void init(int maxValue, int rowCount, int columnCount, int clearableSize,
                     int maxChampion, int generateCount) {
        this.maxValue = maxValue;
        this.rowCount = rowCount;
        this.columnCount = columnCount;
        this.clearableSize = clearableSize;
        this.maxChampion = maxChampion;
        this.generateCount = generateCount;

        checkLinesHome();

        viewFactory = new SwingViewFactory(maxValue);
        championStore = new JaxbChampionsStore(linesHome + File.separator + CHAMPIONS_FILENAME,
                maxChampion);
    }

    private void checkLinesHome() {
        String linesHome = System.getProperty(USER_HOME) + File.separator + LINES_DIR;

        File linesDir = new File(linesHome);
        if (!linesDir.exists()) linesDir.mkdirs();

        this.linesHome = linesHome;
    }

    public IContext createContext() {
        LinesContext context = new LinesContext();
        context.setMaxValue(maxValue);
        context.setRowCount(rowCount);
        context.setColumnCount(columnCount);
        context.setClearableSize(clearableSize);
        context.setMaxChampion(maxChampion);
        context.setGenerateCount(generateCount);
        context.setViewFactory(viewFactory);
        context.setChampionsStore(championStore);

        return context;
    }
}
